package com.wavehi.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author WaveHi
 * @Date 26/9/2021 下午 4:47
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description SuperClass for all servlet.
 */
public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //Unify the character set
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("UTF-8");
        //Get the value of action
        String action = req.getParameter("action");
        //Judge the value of action
        boolean isCorrect = action != null && !"".equals(action);
        //Call the method corresponding to action
        if(isCorrect) {
            try {
                //Get the method corresponding to action
                Method actionMethod = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
                //Open the Access for method
                actionMethod.setAccessible(true);
                //Call the method
                actionMethod.invoke(this, req, resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
